<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="screenName" label="大屏名称" />
        <el-table-column prop="createBy" label="创建者" />
        <el-table-column prop="updateBy" label="更新者" />
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','picWordDataConfig:edit','picWordDataConfig:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />

      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="1020px">
        <el-form ref="form" :model="form" :rules="rules" size="small">
          <el-form-item label="大屏名称">
            <el-input v-model="form.screenName" style="width: 370px;" readonly />
          </el-form-item>
          <el-form-item label="配置信息">
            <el-input v-model="form.data" style="width: 370px;" readonly />
          </el-form-item>
          <el-form-item label="创建者">
            <el-input v-model="form.createBy" style="width: 370px;" readonly />
          </el-form-item>
          <el-form-item label="更新者">
            <el-input v-model="form.updateBy" style="width: 370px;" readonly />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model="form.createTime" style="width: 370px;" readonly />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-input v-model="form.updateTime" style="width: 370px;" readonly />
          </el-form-item>

          <div class="box">
            <div class="tit">
              文图编辑
              <el-button type="primary" @click="addRow">新增一行</el-button>
            </div>
            <div class="table-form">
              <div v-for="(item, index) in imgTextList" :key="'tw' + index" class="item item1">
                <el-form-item label="图片">
                  <el-upload
                    class="avatar-uploader"
                    :action="minioSingleFileUploadApi"
                    :show-file-list="false"
                    :on-success="(res)=>{handleSuccess(res, item, index)}"
                    :on-error="handleError"
                  >
                    <img v-if="item.picUrl" :src="item.picUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon" />
                  </el-upload>
                </el-form-item>
                <el-form-item label="操作">
                  <el-button type="danger" @click="delRow(index)">删除</el-button>
                </el-form-item>
                <el-form-item label="富文本" style="width: 100%">
                  <editor :value="item" />
                </el-form-item>
              </div>
            </div>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudPicWordDataConfig from '@/api/picWordDataConfig'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { Notification } from 'element-ui'
import editor from './components/editor'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, screenName: null, data: null, example: null, createBy: null, updateBy: null, createTime: null, updateTime: null, deleted: null }

const defaultImgTextObj = {
  picUrl: '',
  word: ''
}
export default {
  name: 'PicWordDataConfig',
  // eslint-disable-next-line vue/no-unused-components
  components: { pagination, crudOperation, rrOperation, udOperation, editor },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '图文大屏配置', url: 'api/picWordDataConfig', sort: 'id,desc', crudMethod: { ...crudPicWordDataConfig }})
  },
  computed: {
    ...mapGetters([
      'minioSingleFileUploadApi'
    ])
  },
  data() {
    return {
      permission: {
        add: ['admin', 'picWordDataConfig:add'],
        edit: ['admin', 'picWordDataConfig:edit'],
        del: ['admin', 'picWordDataConfig:del']
      },
      rules: {
      },
      imgTextList: [JSON.parse(JSON.stringify(defaultImgTextObj))]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](data, form) {
      console.log(data, form)
      if (form.data) {
        const val = JSON.parse(form.data)
        this.imgTextList = val
      } else {
        this.imgTextList = [JSON.parse(JSON.stringify(defaultImgTextObj))]
      }
      return true
    },
    [CRUD.HOOK.beforeSubmit]() {
      this.crud.form.data = JSON.stringify(this.imgTextList)
      return true
    },
    handleSuccess(response, row, index) {
      if (response.success) {
        row.picUrl = response.fileUrl
      } else {
        Notification.error({
          title: '上传失败，请稍后再试或联系管理员',
          duration: 3000
        })
      }
    },
    handleError() {
      Notification.error({
        title: '上传失败，请稍后再试或联系管理员',
        duration: 3000
      })
    },
    addRow() {
      this.imgTextList.push(JSON.parse(JSON.stringify(defaultImgTextObj)))
    },
    delRow(index) {
      this.imgTextList.splice(index, 1)
    }
  }
}
</script>

<style lang="scss" scoped>
.box{
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  .tit{
    width: 100%;
    font-size: 18px;
    color: #666;
    text-align: left;
    margin: 12px 0 12px;
  }
  .table-form{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .item{
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 12px;
      ::v-deep .el-form-item{
        width: calc(25% - 20px);
      }
    }
    .item1{
      ::v-deep .el-form-item{
        width: calc(33.3% - 20px);
      }
    }
  }
}

.avatar-uploader {
  ::v-deep.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  ::v-deep.el-upload:hover {
    border-color: #409EFF;
  }
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}

::v-deep .el-dialog__body{
  max-height: 600px;
  overflow: auto;
}

::v-deep .el-form{
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .el-form-item{
    width: calc(50% - 20px);
    display: flex;
    flex-direction: column;
    margin-bottom: 10px;
    margin-right: 20px;
    .el-form-item__label{
      text-align: left;
    }
  }
}
</style>
